import 'package:flutter_test/flutter_test.dart';
import 'package:awak/common/models/content.dart';
import 'package:awak/common/models/topic.dart';
import 'package:awak/common/models/user.dart';
import 'package:awak/page/community/topic_data_holder.dart';

void main() {
  group('Community Updates Tests', () {
    late List<Topic> testTopics;
    late List<Content> testContents;
    late List<User> testUsers;

    setUp(() {
      // 重置状态
      TopicDataHolder.allTopics.clear();
      TopicDataHolder.joinedTopicIds.clear();
      TopicDataHolder.topicPeopleCache.clear();
      TopicDataHolder.currentTopic = null;
      TopicDataHolder.currentTopicId = null;

      // 创建测试数据
      testTopics = [
        Topic(
          id: 1,
          title: 'Philosophy Topic',
          coverImage: 'topic1.jpg',
          content: '3',
          label: 'philosophy',
          numberPeople: 98,
          userId: 1,
        ),
        Topic(
          id: 2,
          title: 'Travel Topic',
          coverImage: 'topic2.jpg',
          content: '2',
          label: 'travel',
          numberPeople: 67,
          userId: 2,
        ),
      ];

      testUsers = [
        User(
          id: 1,
          username: 'alpha',
          password: 'password',
          nickname: 'Alpha User',
          avatar: 'avatar1.jpg',
          signature: 'Philosophy enthusiast',
        ),
        User(
          id: 2,
          username: 'bravo',
          password: 'password',
          nickname: 'Bravo User',
          avatar: 'avatar2.jpg',
          signature: 'Travel lover',
        ),
      ];

      testContents = [
        Content(
          id: 1,
          picture: 'content1.jpg',
          description: 'First philosophical content about reality and existence.',
          userId: 1,
          topicId: 1,
        ),
        Content(
          id: 2,
          picture: 'content2.jpg',
          description: 'Second philosophical content about wisdom and understanding.',
          userId: 2,
          topicId: 1,
        ),
        Content(
          id: 3,
          picture: 'content3.jpg',
          description: 'Travel content about exploring new cultures.',
          userId: 1,
          topicId: 2,
        ),
        Content(
          id: 4,
          picture: 'content4.jpg',
          description: 'Travel content about adventure and discovery.',
          userId: 2,
          topicId: 2,
        ),
      ];

      TopicDataHolder.allTopics = testTopics;
    });

    test('should display all topic contents in community updates', () {
      // 验证所有内容都应该显示
      expect(testContents.length, 4);
      
      // 验证内容来自不同主题
      final topicIds = testContents.map((c) => c.topicId).toSet();
      expect(topicIds.length, 2); // 应该有2个不同的主题
      expect(topicIds.contains(1), true); // 包含哲学主题
      expect(topicIds.contains(2), true); // 包含旅行主题
    });

    test('should group contents by topic correctly', () {
      // 按主题分组内容
      final contentsByTopic = <int, List<Content>>{};
      for (final content in testContents) {
        contentsByTopic.putIfAbsent(content.topicId, () => []).add(content);
      }

      expect(contentsByTopic[1]?.length, 2); // 哲学主题有2个内容
      expect(contentsByTopic[2]?.length, 2); // 旅行主题有2个内容
    });

    test('should associate content with correct user information', () {
      // 验证内容与用户的关联
      for (final content in testContents) {
        final user = testUsers.where((u) => u.id == content.userId).firstOrNull;
        expect(user, isNotNull);
        
        if (content.userId == 1) {
          expect(user?.nickname, 'Alpha User');
          expect(user?.signature, 'Philosophy enthusiast');
        } else if (content.userId == 2) {
          expect(user?.nickname, 'Bravo User');
          expect(user?.signature, 'Travel lover');
        }
      }
    });

    test('should associate content with correct topic information', () {
      // 验证内容与主题的关联
      for (final content in testContents) {
        final topic = testTopics.where((t) => t.id == content.topicId).firstOrNull;
        expect(topic, isNotNull);
        
        if (content.topicId == 1) {
          expect(topic?.title, 'Philosophy Topic');
          expect(topic?.label, 'philosophy');
        } else if (content.topicId == 2) {
          expect(topic?.title, 'Travel Topic');
          expect(topic?.label, 'travel');
        }
      }
    });

    test('should handle mixed content from different topics and users', () {
      // 验证混合内容的处理
      final philosophyContents = testContents.where((c) => c.topicId == 1).toList();
      final travelContents = testContents.where((c) => c.topicId == 2).toList();
      
      expect(philosophyContents.length, 2);
      expect(travelContents.length, 2);
      
      // 验证哲学内容
      expect(philosophyContents.any((c) => c.description.contains('philosophical')), true);
      expect(philosophyContents.any((c) => c.description.contains('reality')), true);
      
      // 验证旅行内容
      expect(travelContents.any((c) => c.description.contains('Travel')), true);
      expect(travelContents.any((c) => c.description.contains('cultures')), true);
    });

    test('should maintain content order and structure', () {
      // 验证内容的顺序和结构
      expect(testContents.first.id, 1);
      expect(testContents.last.id, 4);
      
      // 验证每个内容都有必需的字段
      for (final content in testContents) {
        expect(content.picture.isNotEmpty, true);
        expect(content.description.isNotEmpty, true);
        expect(content.userId, greaterThan(0));
        expect(content.topicId, greaterThan(0));
      }
    });

    test('should support navigation to topic detail from content', () {
      // 验证从内容可以导航到主题详情
      for (final content in testContents) {
        final topic = TopicDataHolder.getTopicById(content.topicId);
        expect(topic, isNotNull);
        expect(topic?.id, content.topicId);
        
        // 模拟设置当前主题
        TopicDataHolder.setCurrentTopic(topic);
        expect(TopicDataHolder.currentTopic?.id, content.topicId);
        expect(TopicDataHolder.currentTopicId, content.topicId);
      }
    });
  });
}
